<?php

require 'Class/autoload.php';
require_once('Class/UpYun/vendor/autoload.php');
use Upyun\Upyun;
use Upyun\Config;



ini_set('display_errors','Off'); 
ini_set('display_errors',1);







date_default_timezone_set("Asia/Chongqing");

//setCookie('uid',1,time()+36000,'/');		//模拟用户登陆，以后删除

$referer = @$_SERVER['HTTP_REFERER'];		//验证网站来源
$p = '/(http:\/\/www.bmxx8.li)|(http:\/\/www.bmxx8.top)/';
if(!preg_match($p,$referer)){
	//exit('10001');
}

$switch_type = @$_REQUEST['type'];					//类别参数

if(@$_COOKIE['uid'] || @$_COOKIE['after_user']){
}else{
	if($switch_type == 'After_Login' || $switch_type == 'Verify_Code' || $switch_type == 'code' || $switch_type=='qq_login' || $switch_type == 'Index_Get_Infos' || $switch_type == 'token' || $switch_type=='Get_Detail_Info' || $switch_type=='GetServiceList'){		//一些需要忽略cookie验证的功能
		
	}else{
		//echo $switch_type;
		exit('10003');
	}
	
}

session_start();


//token验证 注：token存储的session名就是$switch_type
if($switch_type == 'Verify_Code' || $switch_type == 'code' || $switch_type == 'token' || $switch_type == 'qq_login' ){  //判断某些功能要跳过token验证
}else{								
	if(@!$_POST['token'] || @$_SESSION[$switch_type] != @$_POST['token']){
		exit('10002');
	}
}









//判断缓存目录是否存在，并创建 
if(!is_dir('Cache')){
	mkdir('Cache',0777,true);
}
//判断用户数据缓存目录是否存在，并创建 
if(!is_dir('Cache/User')){
	mkdir('Cache/User',0777,true);
}


//分类处理
switch($switch_type){
	//默认
	default :
		echo 'default';

	break;



//===== 后台管理相关

	//刷新所有无限分页中页码与id值的session缓存
	case 'Refresh_Infinite_Fpage_Data_Sess':
		unset($_SESSION['after_page_sess']);
		echo 'ok';
	break;




	


	//服务分类的增加
	case 'After_ServiceType_Add':
		$service_name = $_POST['service_name'];
		$pdo = MyPdo::init();
		$sql = "select type_id from {$config['table_prefix']}service_type where type_name = ?";	//检测服务是否已存在 
		$res = $pdo->dql($sql,array($service_name));
		if($res){
			echo 'err';
		}else{
			$cache_file = 'Cache/GetServiceList';
			$sql2 = "insert into {$config['table_prefix']}service_type(type_name) values(?)";
			$res2 = $pdo->dml($sql2,array($service_name));	//$res2 是添加成功后返回的id值
			if($res2 && $res2 > 0){
				$table_name = $config['table_prefix'].'service'.$res2;
				$sql3 = "create table {$table_name}(id int primary key auto_increment,info_id int comment '信息id')engine=myisam default charset=utf8 comment '服务分类id记录表';";
				$res3 = $pdo->dml($sql3,array());
				$sql4 = "show tables like '{$table_name}'";
				$res4 = $pdo->dql($sql4,array());
				if($res4){
					if(file_exists($cache_file)){
						unlink($cache_file);
					}
					echo 'ok';
				}else{
					$sql5 = "delete from {$config['table_prefix']}service_type where type_id = ?";
					$res5 = $pdo->dml($sql5,array($res2));
					dump($res5);
				}
			}else{
				echo 'err';
			}
		}



	break;


	//服务分类的删除
	case 'After_ServiceType_Del':
		$sid = $_POST['sid'];
		$table_name = $config['table_prefix'].'service'.$sid;
		$pdo = MyPdo::init();
		$sql = "drop table if exists {$table_name}";
		$res = $pdo->dml($sql,array());

		$sql2 = "show tables like '{$table_name}'";
		$res2 = $pdo->dql($sql2,array());
		if(!$res2){
			$sql3 = "delete from {$config['table_prefix']}service_type where type_id = ?";
			$res3 = $pdo->dml($sql3,array($sid));
			if($res3){
				$cache_file = 'Cache/GetServiceList';
				if(file_exists($cache_file)){
					unlink(($cache_file));
				}
				echo 'ok';
			}else{
				echo 'err';
			}
		}else{
			echo 'err';
		}

	break;



	//服务分类的修改
	case 'After_ServiceType_Change':
		$sid = $_POST['sid'];
		$name = $_POST['name'];
		if(!$sid || !$name){
			echo 'err';
		}else{
			$pdo = MyPdo::init();
			$sql = "update {$config['table_prefix']}service_type set type_name=? where type_id = ?";
			$res = $pdo->dml($sql,array($name,$sid));
			if($res){
				$cache_file = 'Cache/GetServiceList';
				if(file_exists($cache_file)){
					unlink(($cache_file));
				}
				echo 'ok';
			}else{
				echo 'err';
			}
		}
	break;



	//服务分类的获取(json字串)
	case 'GetServiceList':
		$pdo = MyPdo::init();
		$sql = "select * from {$config['table_prefix']}service_type";
		$services = $pdo->dql($sql,array());
		foreach($services as $v){
			$infos[] = array($v['type_id'],$v['type_name']);
		}
		echo $data_json = json_encode($infos);
		file_put_contents('Cache/GetServiceList',$data_json);
	break;




	//后台添加关键字
	case 'After_keyword_add':
		$key = $_POST['key'];
		if(!$key){
			echo 'err';
		}else{
			$pdo = MyPdo::init();
			$sql = "select id from {$config['table_prefix']}keyword where content = ?";
			$res = $pdo->dql($sql,array($key),1);
			if($res){
				echo 'err2';
			}else{
				$sql = "insert into {$config['table_prefix']}keyword(content) values(?)";
				$res = $pdo->dml($sql,array($key));
				if($res){
					echo 'ok';
					unset($_SESSION['After_Get_keyword_List_Pnow']);	//添加后重置页码与id记录小额缓存，不然获取数据列表时会不显示新数据
				}else{
					echo 'err3';
				}
			}
		}
	break;


	//获取关键字列表
	case 'After_Get_keyword_List':
		//After_Get_Info_List($_POST['pnow'],$config['table_prefix'].'keyword','id','After_Get_keyword_List_Pnow','','',3);
		$pdo = MyPdo::init();
		$pnow = $_POST['pnow'];
		$fpage_sess_name = 'After_Get_keyword_List';
		$table_name = $config['table_prefix'].'keyword';
		$primary_key = 'id';
		$where = '';
		$is_desc = 'desc';
		$limit = 5;
		$ignore = '';
		$max_page = 10;
		$cache_path = '';
		$query_field = '*';
		Get_Data_For_Fpage_Infinite_b($pdo,$pnow,$fpage_sess_name,$table_name,$primary_key,$where,$is_desc,$limit,$ignore,$max_page,$cache_path,$query_field);
	break;



	//删除关键字
	case 'After_Keyword_Del':
		$id = $_POST['id'];
		if(!$id){
			echo 'err';
		}else{
			$pdo = MyPdo::init();
			$sql = "delete from {$config['table_prefix']}keyword where id=?";
			$res = $pdo->dml($sql,array($id));
			if($res){
				echo 'ok';
			}else{
				echo 'err2';
			}
		}
	break;






	//后台获取封禁理由列表
	case 'After_Get_Close_List':
		//After_Get_Info_List($pnow,$table_name,$max_field,$page_session_name,$where_field,field,$limit,$desc);
		After_Get_Info_List($_POST['pnow'],$config['table_prefix'].'close_cause','id','After_Get_Close_List_Pnow','','',3);
	break;



	//删除理由 
	case 'After_Close_Del':
		$id = $_POST['id'];
		if(!$id){
			echo 'err';
		}else{
			$pdo = MyPdo::init();
			$sql = "delete from {$config['table_prefix']}close_cause where id=?";
			$res = $pdo->dml($sql,array($id));
			if($res){
				echo 'ok';
			}else{
				echo 'err2';
			}
		}
	break;





	//后台添加封禁理由
	case 'After_close_add':
		$content = $_POST['close_text'];
		if(!$content){
			echo 'err';
		}else{
			$pdo = MyPdo::init();
			$sql = "select id from {$config['table_prefix']}close_cause where content = ?";
			$res = $pdo->dql($sql,array($content),1);
			if($res){
				echo 'err2';
			}else{
				$sql = "insert into {$config['table_prefix']}close_cause(content) values(?)";
				$res = $pdo->dml($sql,array($content));
				if($res){
					echo 'ok';
					unset($_SESSION['After_Get_Close_List_Pnow']);	//添加后重置页码与id记录小额缓存，不然获取数据列表时会不显示新数据
				}else{
					echo 'err3';
				}
			}
		}
	break;




	//后台信息管理-获取信息列表
	case 'After_Get_Info_List':
		$pnow = $_POST['pnow'];
		$field = 'info_id,info_uid,info_service,info_stat,info_auto_issue,info_title,info_content,info_contacts,info_phone,info_issue_time,info_imgs';
		$max_field = 'info_id';		//查询max()最大值的主键字段名
		$table_name = $config['table_prefix'].'info';	//表名
		$page_session_name = 'After_Get_Info_List_Page';	//设置当前要使用的分页session缓存名
		$where_field = 'info_stat';	//需要where 查询的字段名
		After_Get_Info_List($pnow,$table_name,$max_field,$page_session_name,$where_field,'',10);
	break;






	//修改后台管理员的所属组
	case 'After_User_Change_Group':
		$gid = $_POST['gid'];
		$gname = $_POST['gname'];
		$uid = $_POST['uid'];
		if(!$gid || !$gname || !$uid){
			echo 'err';
		}else{
			$pdo = MyPdo::init();
			$sql = "update {$config['table_prefix']}admin_user set group_id=?,group_name=? where id=?";
			$res = $pdo->dml($sql,array($gid,$gname,$uid));
			if($res){
				echo 'ok';
			}else{
				echo 'err2';
			}
		}
	break;





	//获取分组与该 组的权限
	case 'After_Get_GroupOrFuns':
		$pdo = MyPdo::init();
		$sql = "select * from {$config['table_prefix']}admin_group";
		$res = $pdo->dql($sql,array());
		if(!$res){
			echo 'err';
		}else{
			//dump($res[0]['id'],1);
			foreach ($res as $k=>$v) {
				$arr = explode(',',$v['privilege']);

				$name_str = '';
				foreach($arr as $v2){
					$name_str .= "'".$v2."',";
				}
				$name_str = trim($name_str,',');

				$sql2 = "select * from {$config['table_prefix']}admin_privilege where name in({$name_str})";
				$res2 = $pdo->dql($sql2,array());
				if($res2){
					$l1 = $l2 = array();

					foreach($res2 as $k3=>$v3){
						if($v3['parent'] == 0){
							$l1[] = $v3;
						}else{
							$l2[] = $v3;
						}
					}
					if(count($l1) > 0 && count($l2) > 0){
						foreach($l1 as $k4=>$v4){
							foreach($l2 as $v5){
								if($v5['parent'] == $v4['id']){
									$l1[$k4]['sub'][] = $v5;
								}
							}
						}
					}
					if(count($l1) > 0){
						$res[$k]['pList'] = $l1;

					}

				}
			}
			//		dump($res);
			echo json_encode($res);
		}


	break;






	//删除后台管理组中的组员
	case 'After_Group_For_User_Del':
		$uid = $_POST['uid'];
		if(!$uid){
			echo 'err';
		}else{
			$pdo = MyPdo::init();
			$sql = "update {$config['table_prefix']}admin_user set group_id='',group_name='' where id=?";
			$res = $pdo->dml($sql,array($uid));
			if($res){
				echo 'ok';
			}else{
				echo 'err2';
			}
		}


	break;







	//获取后台管理的组用户列表
	case 'After_Get_Group_User':
		$gid = $_POST['gid'];
		if(!$gid){
			echo 'err';
		}else{
			$pdo = MyPdo::init();
			$sql = "select * from {$config['table_prefix']}admin_user where group_id=? order by id desc";
			$res = $pdo->dql($sql,array($gid));
			if(!$res){
				echo 'err';
			}else{
				echo json_encode($res);
			}
		}
	break;





	//设置后台分组的权限
	case 'Add_Fun_For_Group':
		$identifys = $_POST['ids'];
		$gid = $_POST['gid'];
		if($identifys && $gid){
			if($identifys != 'none'){
				$identifys = implode($identifys,',');
			}else{
				$identifys = '';				
			}
			$pdo = MyPdo::init();
			$sql = "update {$config['table_prefix']}admin_group set privilege = ? where id = ?";
			$res = $pdo->dml($sql,array($identifys,$gid));
			if($res){
				echo 'ok';
			}else{
				echo 'err1';
			}

		}else{
			echo 'err2';
		}

	break;





	//后台组管理中获取功能全部数据
	case 'After_Cat_Group_Grant':
		$gid = $_POST['gid'];
		if(!$gid){
			echo 'err';
		}else{
			$pdo = MyPdo::init();
			//获取全部功能列表
			$sql = "select * from {$config['table_prefix']}admin_privilege";
			$res = $pdo->dql($sql,array());
			if(!$res){
				echo 'err';
			}else{
				foreach($res as $v){
					$data['funs'][] = array($v['id'],$v['name'],$v['explains'],$v['parent']);
				}
				//获取指定分组拥有的权限功能列表 
				$sql = "select privilege from {$config['table_prefix']}admin_group where id = ?";
				$res = $pdo->dql($sql,array($gid),1);
				$data['privilege'] = explode(',',$res['privilege']);
				//dump($data);
				echo json_encode($data);
			}
		}
	break;



	//后台管理的分组删除
	case 'After_Group_Del':
		$gid = $_POST['gid'];
		if(!$gid){
			echo 'err';
		}else{
			$pdo = MyPdo::init();
			$sql = "delete from {$config['table_prefix']}admin_group where id = ?";
			$res = $pdo->dml($sql,array($gid));
			if($res){
				echo 'ok';
			}else{
				echo 'err';
			}
		}


	break;




	//获取后台管理分组数据
	case 'After_Get_Group_List':
		$pdo = MyPdo::init();
		$sql = "select * from {$config['table_prefix']}admin_group";
		$res = $pdo->dql($sql,array());
		if(!$res){
			echo 'err';
		}else{
			foreach($res as $v){
				$data[] = array($v['id'],$v['name'],$v['explains'],$v['privilege']);
			}
			echo json_encode($data);
		}
	break;



	//添加后台管理分组
	case 'After_Admin_Group_Add':
		$group = $_POST['group'];
		$explain = $_POST['explain'];
		if(!$group || !$explain){
			echo 'err';
		}else{
			$pdo = MyPdo::init();
			$sql = "select id from {$config['table_prefix']}admin_group where name = ?";
			$res = $pdo->dql($sql,array($group));
			if($res){
				echo 'err';
			}else{
				$sql = "insert into {$config['table_prefix']}admin_group(name,explains) values(?,?) ";
				$res = $pdo->dml($sql,array($group,$explain));
				if($res){
					echo 'ok';
				}else{
					echo 'err';
				}
			}
		}
	break;



	//删除后台页面或功能标识
	case 'After_Fun_Del':
		$fid = $_POST['fid'];
		$page_stat = $_POST['page_stat'];
		if(!isset($fid) || !isset($page_stat)){
			echo 'err';
		}else{
			if($page_stat == 'page'){
				$sql = "delete from {$config['table_prefix']}admin_privilege where id = ? || parent = ?";
				$arr = array($fid,$fid);
			}else{
				$sql = "delete from {$config['table_prefix']}admin_privilege where id = ?";
				$arr = array($fid);
			}
			$pdo = MyPdo::init();
			$res = $pdo->dml($sql,$arr);
			if($res){
				echo 'ok';
			}else{
				echo 'err';
			}
		}
	break;



	//获取功能列表
	case 'After_Get_Fun_List':
		$pdo = MyPdo::init();
		$sql = "select * from {$config['table_prefix']}admin_privilege order by parent,id";
		$res = $pdo->dql($sql,array());
		if(!$res){
			echo 'err';
		}else{
			foreach($res as $k=>$v){
				$data[$k] = array($v['id'],$v['name'],$v['explains'],$v['parent']);
			}
			echo json_encode($data);
		}
	break;



	//添加权限表的功能或页面
	case 'Admin_fun_Add':
		$name = $_POST['name'];
		$explain = $_POST['explain'];
		$parent = $_POST['parent'];
		if(!$name || !$explain || !isset($parent)){
			echo 'err';
		}
		$pdo = MyPdo::init();
		$sql = "select * from {$config['table_prefix']}admin_privilege where name=?";
		$res = $pdo->dql($sql,array($name));
		if($res){
			echo 'err';
		}else{
			$sql = "insert into {$config['table_prefix']}admin_privilege values(null,?,?,?)";
			$res = $pdo->dml($sql,array($name,$explain,$parent));
			if($res){
				echo 'ok';
			}else{
				echo 'err';
			}
		}

	break;



	//获取功能设置时要选择页面信息
	case 'After_Fun_Get_Page_Data':
		$pdo = MyPdo::init();
		$sql = "select id,name,explains from {$config['table_prefix']}admin_privilege where parent = 0";
		$res = $pdo->dql($sql,array());
		if(!$res){
			echo 'err';
		}else{
			foreach($res as $k=>$v){
				$data[$k] = array($v['id'],$v['name'],$v['explains']);
			}
			echo json_encode($data);
		}
	break;


	//删除后台管理员
	case 'After_User_Del':
		$uid = $_POST['uid'];
		if(!$uid){
			echo 'err';
		}else{
			$pdo=MyPdo::init();
			$sql = "delete from {$config['table_prefix']}admin_user where id = ?";
			$res = $pdo->dml($sql,array($uid));
			if($res){
				echo 'ok';
			}else{
				echo 'err';
			}
		}
	break;


	//修改后台管理员密码
	case 'after_change_user_pwd':
		$uid = trim($_POST['uid']);
		$pwd = trim($_POST['pwd']);
		if(!$uid || !$pwd){
			echo 'err';
		}else{
			$sql = "update {$config['table_prefix']}admin_user set pwd = ? where id = ?";
			$pdo  = MyPdo::init();
			$res = $pdo->dml($sql,array(md5($pwd),$uid));
			if($res){
				echo 'ok';
			}else{
				echo 'err';
			}
		}

	break;



	//获取管理员列表
	case 'After_Get_User_List':
		$pdo = MyPdo::init();
		$sql = "select id,name,explains,group_id,group_name,ctime from {$config['table_prefix']}admin_user order by id desc";
		$res = $pdo->dql($sql,array());
		if($res){
			foreach($res as $k=>$v){
				$user_list[] = array($v['id'],$v['name'],$v['group_id'],$v['group_name'],$v['ctime'],$v['explains']);	
			}
			echo json_encode($user_list);
		}else{
			echo 'err';
		}
	break;


	//添加管理员
	case 'admin_user_add':
		$user = $_POST['user'];
		$explain = @$_POST['explain'];
		if(!$user){
			echo 'err';
		}
		$pdo = MyPdo::init();
		$sql = "select id from {$config['table_prefix']}admin_user where name = ?";
		$res = $pdo->dql($sql,array($user));
		if($res){
			echo 'err2';
		}else{
			if($explain){
				$sql = "insert into {$config['table_prefix']}admin_user(name,explains,pwd) values(?,?,?)";
				$arr = array($user,$explain,md5(123));
			}else{
				$sql = "insert into {$config['table_prefix']}admin_user(name,pwd) values(?,?)";
				$arr = array($user,md5(123));
			}
			$res = $pdo->dml($sql,$arr);
			if($res){
				echo 'ok';
			}else{
				echo 'err3';
			}
		}
	break;



	//后台退出
	case 'admin_exit':
		foreach($_COOKIE as $k=>$v){
			setCookie($k,'',time()-1,'/');
		}
		unset($_SESSION);
	break;



	//后台登陆
	case 'After_Login':
		//setCookie('after_user','admin',time()+6000,'/');
		//echo 'ok';
		//require 'Require/After_Login.php';
		if(!Verify_Code()){
			exit('e1');
		}
		$name = $_POST['user'];
		$pwd = $_POST['pwd'];

		if(!$name || !$pwd){
			exit('e2');
		}
		$stat = false;
		if($name == 'admin'){
			if(md5($pwd) == md5('123')){
				$stat = true;
			}
		}else{
			$pdo = MyPdo::init();
			$sql = "select pwd from {$config['table_prefix']}admin_user where name = ?";
			$res = $pdo->dql($sql,array($name),1);
			if(md5($pwd) == $res['pwd']){
				$stat = true;
			}
		}
		if($stat){
			setCookie('after_user',$name,time()+6000,'/');
			echo 'ok';
		}else{
			echo 'e2';
		}
	break;


	//获取前台用户列表
	case 'After_Get_Before_User_List':
		//require 'Require/After_Get_Before_User_List.php';
		$pnow = intval(@$_POST['pnow']);
		$user_stat = @$_POST['user_stat'];

		$psize = 10;	//设置每页显示多少条

		$where = array(); 
		if(@$_POST['uid'] || $user_stat == 'all'){
			$sess_name = 'After_Get_Before_User_List';
			if($user_stat != 'all'){
				array_push($where,array('user_id',$_POST['uid'],'='));
				$pnow = 1;
			}
			$table_name = $config['table_prefix'].'user';
			$primary_key = 'user_id';
			$query_field = 'user_id,user_name,user_ctime,user_ltime,user_stat';
			$ignore = '';
			$max_page = 10;
			$desc = 'desc';
			$cache_path = '';
			$pdo = MyPdo::init();
			//dump($_POST,1);
			Get_Data_For_Fpage_Infinite_b($pdo,$pnow,$sess_name,$table_name,$primary_key,$where,$desc,$psize,$ignore,$max_page,$cache_path,$query_field);
		}else{
			$sess_name = 'After_Get_Before_User_List'.$user_stat;
			$primary_key = 'id';
			$query_field = 'uid';
			switch($user_stat){
				case '1':
				case 1:
					$table_name = $config['table_prefix'].'user_normal';break;
				case '2':
				case 2:
					$table_name = $config['table_prefix'].'user_close';break;
			}
			//dump($where,1);
			$ignore = '';
			$max_page = 10;
			$desc = 'desc';
			$cache_path = '';
			$pdo = MyPdo::init();
			//dump($_POST,1);
			$res = Get_Data_For_Fpage_Infinite_b($pdo,$pnow,$sess_name,$table_name,$primary_key,$where,$desc,$psize,$ignore,$max_page,$cache_path,$query_field,1);
		//	echo $res;
			$res2 = json_decode($res,1);
			$data = $res2['data'];
			$ids_str = '';
			foreach($data as $v){
				$ids_str .= $v['uid'].',';
			}
			$ids_str = trim($ids_str,',');	//获取到id 串
			if($ids_str){
				$sql3 = "select user_id,user_name,user_ctime,user_ltime,user_stat from {$config['table_prefix']}user where user_id in ({$ids_str})";
				$res3 = $pdo->dql($sql3,array());
				$res2['data'] = $res3;
				echo json_encode($res2);
			}
		}
	break;



	//用户封禁解禁操作
	case 'After_Change_User_Stat':
		//require 'Require/After_Change_User_Stat.php';
		$uid = $_POST['uid'];
		$stat = $_POST['stat'];
		if(!$uid || !$stat){
			exit;
		}
		$pdo = MyPdo::init();
		if($stat == 1){
			$user_stat = 2;
			$sql2 = "delete from {$config['table_prefix']}user_normal where uid = ?";
			$sql3 = "insert into {$config['table_prefix']}user_close(uid) values(?)";
		}else{
			$user_stat = 1;
			$sql2 = "delete from {$config['table_prefix']}user_close where uid = ?";
			$sql3 = "insert into {$config['table_prefix']}user_normal(uid) values(?)";
		}
		$sql = "update {$config['table_prefix']}user set user_stat=? where user_id=?";
		$res = $pdo->dml($sql,array($user_stat,$uid));
		if($res && $res > 0){
			$res2 = $pdo->dml($sql2,array($uid));
			$res3 = $pdo->dml($sql3,array($uid));
			if($res3){
				unset($_SESSION['after_page_sess']);
				echo 'ok';
			}else{
				echo 'err1';
			}
		}else{
			echo 'err3';
		}
	break;


	//获取用户登陆记录
	case 'After_Get_User_Login_Record':
		//require 'Require/After_Get_User_Login_Record.php';
		$uid = $_POST['uid'];
		$pnow = $_POST['pnow'];
		if(!$uid){
			exit('err');
		}
		$where = array(); 
		array_push($where,array('uid',$_POST['uid'],'='));
		$ignore = '';
		$max_page = 10;
		$desc = 'desc';
		$cache_path = '';
		$pdo = MyPdo::init();
		$query_field = 'id,ip,time';
		$sess_name = 'After_Get_User_Login_Record';
		//echo $pnow;exit;
		Get_Data_For_Fpage_Infinite_b($pdo,$pnow,$sess_name,$config['table_prefix'].'login_record','id',$where,$desc,10,$ignore,$max_page,$cache_path,$query_field);

	break;





	//获取用户发布记录
	case 'After_Get_User_Issue_Info':
		$pnow = intval($_POST['pnow']);

		$where = array();
		if(isset($_POST['uid'])){
			$uid = $_POST['uid'];
			array_push($where,array('info_uid',$uid,'=')); 
		}
		if(@$_POST['search']){
			array_push($where,array('info_title',$_POST['search'],'like')); 
		}
		$info_stat = $_POST['info_stat'];
		if($info_stat != 'all'){
			array_push($where,array('info_stat',$info_stat,'='));
		}
		$ignore = 'ignore index(uid)';
		$max_page = 10;
		$desc = 'desc';
		$cache_path = '';
		$pdo = MyPdo::init();
		$query_field = '*';
		Get_Data_For_Fpage_Infinite_b($pdo,$pnow,'After_Get_User_Issue_Info',$config['table_prefix'].'info','info_id',$where,'desc',20,$ignore,$max_page,$cache_path,$query_field);
	break;




	//修改信息状态
	case 'After_Change_Info_Stat':
		$info_id = $_POST['info_id'];
		$info_uid = $_POST['info_uid'];
		$info_stat = $_POST['info_stat'];
		$con_type = $_POST['con_type'];
		if(!isset($info_id) || !isset($info_stat)){
			exit('err');
		}
		if($con_type != 'none'){
			if($con_type == 'deny'){
				$info_stat = 3;
			}else{
				$info_stat = 1;
			}
		}
		//dump($info_stat,1);
		//$sql = "select info_stat from {$config['table_prefix']}info where info_id = ?";
		$sql = "update {$config['table_prefix']}info set info_stat = ? where info_id = ?";

		$pdo = MyPdo::init();
		$res = $pdo->dml($sql,array($info_stat,$info_id));
		if($res){
			echo 'ok';
			$change_arr = array(
				array(5,$info_stat)
			);
			Cache_Modify('Cache/User/'.$info_uid,$change_arr,0,$_POST['info_id']);
			$detail_file = 'Cache/Detail/'.$_POST['info_id'];
			if(file_exists($detail_file)){
				unlink($detail_file);
			}
		}else{
			echo 'err2';
		}

	break;




//===== index 页相关

	//获取信息列表

	case 'Index_Get_Infos':
		//判断信息列表缓存目录是否存在，并创建 
		if(!is_dir('Cache/Info_List')){
			mkdir('Cache/Info_List',0777,true);
		}
		$pdo = MyPdo::init();
		$service = $_POST['info_service'];
		if($service == 'all'){
			$sql3 = "select info_id from {$config['table_prefix']}info order by info_id desc limit 10000,1";
			$res3 = $pdo->dql($sql3,array(),1);
			if($res3['info_id'] && $res3['info_id'] > 0){
				//echo $res3['info_id'];
				$where_info_id = " and info_id > ".$res3['info_id'];
			}else{
				$where_info_id = "";
			}

		}else{
			$table_name = $config['table_prefix'].'service'.$service;
			$sql2 = "select info_id from {$table_name} order by id desc limit 10000";
			$res_ids = $pdo->dql($sql2,array());
			if(!$res_ids){
				goto a;
			}
			$ids_str = '';
			foreach ($res_ids as $v) {
				$ids_str .= $v['info_id'].',';
			}
			$ids_str2 = trim($ids_str,',');
			$where_info_id = " and info_id in({$ids_str2})";
		}
		//拼接数据查询sql并执行并缓存
		$sql = "select info_id,info_title,info_issue_time,info_service,info_auto_issue from {$config['table_prefix']}info where info_stat in (1,2) {$where_info_id} order by info_stat,info_auto_issue desc,info_id desc limit 1000";
		//echo $sql;exit;
		$res = $pdo->dql($sql,array());
		if($res){
			foreach($res as $v){
				$data[] = array($v['info_id'],$v['info_title'],$v['info_issue_time'],$v['info_service'],$v['info_auto_issue']);
			}
			$path = 'Cache/Info_List/service_'.$service;
			Merge_Or_Make_Cache2($data,$path);
		}

		a:
	break;



/*	case 'Index_Get_Infos':
		//判断信息列表缓存目录是否存在，并创建 
		if(!is_dir('Cache/Info_List')){
			mkdir('Cache/Info_List',0777,true);
		}
		$pdo = MyPdo::init();

		//判断是否倒查
		if(!isset($_SESSION['desc_count'])){
			$_SESSION['desc_count'] = Get_NormalOrDescLimitId($pdo,$config['table_prefix'].'info','info_id',$config['desc_count']);
		}
		//echo $_SESSION['desc_count'];exit;
		if($_SESSION['desc_count'] > 0){
			$where_info_id = "and info_id > {$_SESSION['desc_count']}";
		}else{
			$where_info_id = "and info_id > 0";
		}
		//dump($_POST['info_service'],1);
		$service = $_POST['info_service'];
		if($service == 'all'){
			$where_service = '';
		}else{
			$where_service = " and info_service = '{$service}'";
		}

		//拼接数据查询sql并执行并缓存
		$sql = "select info_id,info_title,info_issue_time,info_service,info_auto_issue from {$config['table_prefix']}info where info_stat in (1,2) {$where_service} {$where_info_id} order by info_stat,info_auto_issue desc,info_id desc limit 1000";
		$res = $pdo->dql($sql,array());
		if($res){
			foreach($res as $v){
				$data[] = array($v['info_id'],$v['info_title'],$v['info_issue_time'],$v['info_service'],$v['info_auto_issue']);
			}
			$path = 'Cache/Info_List/service_'.$service;
			Merge_Or_Make_Cache2($data,$path);
		}

	break;*/












//===== 详情页相关功能

	//-- 获取要编辑信息
	case 'Get_Detail_Info':
		//require 'Require/Get_Detail_Info.php';
		$iid = $_POST['iid'];
		$pdo = MyPdo::init();
		$sql = "select info_id,info_uid,info_service,info_title,info_content,info_contacts,info_phone,info_imgs,info_city_xian,info_stat,info_auto_issue,info_issue_time from {$config['table_prefix']}info where info_id=?";
		$res = $pdo->dql($sql,array($iid),1);
		if($res){
			foreach($res as $v){
				$res2[] = $v;
			}

			echo $data = json_encode($res2);

			//判断详情据缓存目录是否存在，并创建 
			if(!is_dir('Cache/Detail')){
				mkdir('Cache/Detail',0777,true);
			}
			
			file_put_contents('Cache/Detail/'.$iid, $data);

		}else{
			echo  'err';
		}
	break;







//===== me_edit 页相关功能 

	//-- 获取要编辑信息
	case 'get_edit_info':
		//require 'Require/Get_Detail_Info.php';
		$iid = $_POST['iid'];
		$pdo = MyPdo::init();
		$sql = "select info_id,info_uid,info_service,info_title,info_content,info_contacts,info_phone,info_imgs,info_city_xian,info_stat,info_auto_issue,info_issue_time from {$config['table_prefix']}info where info_id=?";
		$res = $pdo->dql($sql,array($iid),1);
		if($res){
			foreach($res as $v){
				$res2[] = $v;
			}

			echo $data = json_encode($res2);

			//判断详情据缓存目录是否存在，并创建 
			if(!is_dir('Cache/Detail')){
				mkdir('Cache/Detail',0777,true);
			}
			
			file_put_contents('Cache/Detail/'.$iid, $data);

		}else{
			echo  'err';
		}
	break;



	//-- 信息编辑的处理
	case 'Info_Submit_Edit':
		$serviceConfig = new Config('tp-service', 'tpcwre', 'xiaodong123');
		$up = new Upyun($serviceConfig);
		//require 'Require/Info_Submit_Edit.php';
		if(!$_COOKIE['uid'] || !$_POST['uid'] || $_COOKIE['uid'] !== $_POST['uid']){
			exit('err1');
		}
		$img_uri = $config['upyun_src'];		//图片域名和uri
		//$up = new UpYun();
		//$serviceConfig = new Config('tp-service', 'tpcwre', 'xiaodong123');
		//$up = new Upyun($serviceConfig);
		$img_arr = array();
		foreach ($_FILES as $v) {
			$folder = '/bmxx8/';
			$ext = pathinfo($v['name'],PATHINFO_EXTENSION); //文件后缀
			$file_name = md5(microtime(1).rand(1,9999999)).'.'.$ext;	//拼接文件名
			$path = $folder.$file_name;	//拼接完整上传路径
			$res = $up->write($path,file_get_contents($v['tmp_name']));
			$img_ext = Check_File_Size($v);
			if($res){
				$img_arr[] = $img_uri.$path.$img_ext;
 			}
		}
		if(@$_POST['origin_imgs'] && strlen($_POST['origin_imgs']) > 0){
			foreach(json_decode($_POST['origin_imgs'],1) as $v){
				array_unshift($img_arr,$v);
			}
		}

		if(count($img_arr) > 0){
			$_POST['info_imgs'] = json_encode($img_arr);	//将上传后的图片地址解析成json并存入post数组
		}

		//一：先计算修改后的图片差异，并删除upyun图片实体
		if(@$_POST['origin_imgs2']){
			$img_arr = json_decode($_POST['origin_imgs'],1);	//修改后的图片集合
			$img_arr2 = json_decode($_POST['origin_imgs2'],1);	//修改前的图片集合
			foreach(array_diff($img_arr2,$img_arr) as $v){		//比较差异，选出被丢弃的图片并删除
				$uri = parse_url($v)['path'];		//获取到修改时被删除的原图片uri(目录+文件名)
				preg_match_all('/[^!]*/',$uri, $res);
				$res2 = $up->delete($res[0][0]);
			}
		}

		if(@!$_POST['info_imgs']){		//如果没图片就置空，以便修改bmxx8_info表时不出错
			$_POST['info_imgs']='';
		}

		$pdo = MyPdo::init();

		//unset($_POST['auto_send']);
		unset($_POST['token']);
		unset($_POST['type']);
		unset($_POST['uid']);
		unset($_POST['origin_imgs']);
		unset($_POST['origin_imgs2']);


		//检测指定内容是否匹配关键词表中的关键词
		if(Check_Keywords($pdo,array($_POST['info_title'],$_POST['info_content'],$_POST['info_contacts']))){
			$_POST['info_stat'] = 4;	//匹配到封禁关键字则，状态为4的审核中
		}else{
			$_POST['info_stat'] = 0;	//未匹配到封禁关键字则，状态为0的审核中
		}

		//echo '<pre>';var_dump($_POST);exit;

		//三：修改bmxx8_info表 
		$sql = "update {$config['table_prefix']}info set info_title=?,info_content=?,info_contacts=?,info_phone=?,info_auto_issue=?,info_imgs=?,info_stat=? where info_id=? and info_uid=?";
		$arr = array($_POST['info_title'],$_POST['info_content'],$_POST['info_contacts'],$_POST['info_phone'],$_POST['info_auto_issue'],$_POST['info_imgs'],$_POST['info_stat'],$_POST['info_id'],$_COOKIE['uid']);
		//echo $sql;
		//dump($arr,1);
		$res = $pdo->dml($sql,$arr);
		if($res > 0){
			echo 'ok';
			//info_id,info_service,info_title,info_issue_time,info_auto_issue,info_stat
			$change_arr = array(
				array(1,$_POST['info_service']),
				array(2,$_POST['info_title']),
				array(4,$_POST['info_auto_issue']),
				array(5,$_POST['info_stat'])
			);

			//修改用户信息列表静态缓存
			Cache_Modify('Cache/User/'.$_COOKIE['uid'],$change_arr,0,$_POST['info_id']);


			$change_detail = array(
				array('3',$_POST['info_title']),
				array('4',$_POST['info_content']),
				array('5',$_POST['info_contacts']),
				array('6',$_POST['info_phone']),
				array('7',$_POST['info_imgs']),
				array('11',$_POST['info_auto_issue'])
			);
			//修改我发布的信息列表静态缓存
			Cache_Modify('Cache/Detail/'.$_POST['info_id'],$change_detail);
			//删除详情静态缓存
			$detail_file = 'Cache/Detail/'.$_POST['info_id'];
			if(file_exists($detail_file)){
				unlink($detail_file);
			}

		}else{
			echo 'err';
		}
	break;





//===== me 页相关功能 

	//-- 删除信息
	case 'Del_Me_Info':
		$iid = $_POST['iid'];
		$uid = $_POST['uid'];
		if(!$iid || !$uid){
			exit('err1');
		}
		//echo $iid;exit;
		$pdo = MyPdo::init();
		$sql = "update {$config['table_prefix']}info set info_stat=2,info_auto_issue=0 where info_id=? and info_uid = ?";
		$res = $pdo->dml($sql,array($iid,$uid));
		if($res){
			echo 'ok';
			//修改用户发布信息列表缓存
			Cache_Modify('Cache/User/'.$uid,array(array(4,0),array(5,2)),0,$iid);
			
			//修改信息详情缓存（待完成）

			$change_detail = array(
				array('9',2),
				array('11',0),
			);
			Cache_Modify('Cache/Detail/'.$iid,$change_detail);

		}else{
			echo 'err2';
		}
	break;


	//-- 获取我发布的信息
	case 'Get_Me_Info':
		//require 'Require/Get_Me_Info.php';
		$Pdo = MyPdo::init();
		$Uid = @$_POST['uid'];
		if(!$Uid){
			exit('err');
		}
		//获取倒查或正查的起始值
		$LimitStart = Get_NormalOrDescLimitId($Pdo,$config['table_prefix'].'info','info_id',$config['desc_count']);
		if($LimitStart > 0){	//倒查指定范围
			$where = "and info_id > {$LimitStart}";
		}else{		//正查
			$where = "";
		}
		$Sql = "select info_id,info_service,info_title,info_issue_time,info_auto_issue,info_stat from {$config['table_prefix']}info where info_uid = ? {$where} order by info_id desc limit 100";
		$Res = $Pdo->dql($Sql,array($Uid));
		$data = Array();
		foreach($Res as $v){
			$arr = Array();
			foreach($v as $vv){
				$arr[] = $vv;	
			}
			$data[] = $arr;
		}
		if($Res){
			echo $InfoJson = json_encode($data);
			file_put_contents('Cache/User/'.$Uid, $InfoJson);
		}
	break;












//===== issue 页相关功能 

	//信息发布提交的处理
	case 'info_submit':
		
		$pdo = MyPdo::init();

		//判断是正查还是倒查
		$LimitStart = Get_NormalOrDescLimitId($pdo, $config['table_prefix'].'info', 'info_id',$config['desc_count']);
		if($LimitStart > 0){
			$where_info_id = "and info_id > {$LimitStart}";
		}else{
			$where_info_id = '';
		}

		//判断用户当天是否在该服务下发布过信息
		$sql = "select info_id from {$config['table_prefix']}info where info_uid=? and info_service=? and info_issue_time > ? {$where_info_id} limit 1";
		$res = $pdo->dql($sql,array($_POST['info_uid'],$_POST['info_service'],GetDateYmd('-0 day')));
		//dump($res,1);
		if($res){
			if($_COOKIE['uid'] != 1){
				exit('e2');		//每天只能发一条
			}
		}

		$img_uri = $config['upyun_src'];		//图片域名和uri
		//$up = new UpYun();
		$serviceConfig = new Config('tp-service', 'tpcwre', 'xiaodong123');
		$up = new Upyun($serviceConfig);
		$img_arr = array();
		foreach ($_FILES as $v) {
			$folder = '/bmxx8/';
			$ext = pathinfo($v['name'],PATHINFO_EXTENSION); //文件后缀
			$file_name = md5(microtime(1).rand(1,9999999)).'.'.$ext;	//拼接文件名
			$path = $folder.$file_name;	//拼接完整上传路径

			$img_ext = Check_File_Size($v);
			$res = $up->write($path,file_get_contents($v['tmp_name']));
			if($res){
				$img_arr[] = $img_uri.$path.$img_ext;
 			}
		}
		if(count($img_arr) > 0){
			$_POST['info_imgs'] = json_encode($img_arr);	//将上传后的图片地址解析成json并存入post数组
		}
		unset($_POST['token']);
		unset($_POST['type']);
		$_POST['info_stat'] = 0;		//信息显示状态,默认为审核状态
		if($_COOKIE['uid'] == 1){
			$_POST['info_stat'] = 1;
		}
		$_POST['info_issue_time'] = time();
		//$_POST['info_view_time'] = GetDateYmd('-0 day');	//设置为当天的零时时间戳
		$issue_stat = 'ok';		//标记发布信息成功状态
		//添加数据到bmxx8_info 信息表
		$keys_str = implode(',',array_keys($_POST));
		$vals_str = implode(',',array_fill(0,count(array_keys($_POST)),'?'));



		//检测关键词
		if(Check_Keywords($pdo,array($_POST['info_title'],$_POST['info_content'],$_POST['info_contacts']))){
			$_POST['info_stat'] = 4;
		}


		$sql = "insert into {$config['table_prefix']}info(".$keys_str.") values(".$vals_str.")";
		$info_new_id = $pdo->dml($sql,array_values($_POST));
		$file_name = 'Cache/User/'.$_POST['info_uid'];	//缓存文件名
		

		if($info_new_id > 0){
			//将发布的信息id 添加到分类服务的id记录表中
			$service_table_name = $config['table_prefix'].'service'.$_POST['info_service'];

			$sql_create_table = "create table if not exists {$service_table_name}(id int primary key auto_increment,info_id int comment '信息id')engine=myisam default charset=utf8 comment '服务分类id记录表';";
			$res_create_table = $pdo->dml($sql_create_table,array());

			$sql_service_record = "insert into {$service_table_name}(info_id) values(?)";
			$res_service_record = $pdo->dml($sql_service_record,array($info_new_id));
			//dump($res_service_record,1);


			$add_cache_arr = array($info_new_id,$_POST['info_service'],$_POST['info_title'],$_POST['info_issue_time'],$_POST['info_auto_issue'],$_POST['info_stat']);
			//新发布的信息添加到缓存
			Add_Cache_Item($file_name,$add_cache_arr); 

			//将新发布的信息id,uid 添加了用户发布记录表
			$sql = "insert into {$config['table_prefix']}issue_record(uid,info_id) values(?,?)";
			$res = $pdo->dml($sql,array($_POST['info_uid'],$info_new_id));
			if(!$res){
				$issue_stat = 'e2';//信息发布成功，但用户发布记录表添加失败
			}
		}else{
			$issue_stat = 'e1';		//信息发布失败
		}
		echo $issue_stat;
	break;












//===== 其它功能



	//页面及功能验证
	case 'Priv_Check':
	$after_user = $_COOKIE['after_user'];
	if($after_user == 'admin'){
		echo 'ok';
	}else{
		$priv_name = $_POST['priv_name'];
		if(!$after_user || !$priv_name){
			echo 'err1';
		}else{
			if(!@$_SESSION['privileges']){
				$pdo = MyPdo::init();
				$sql = "select g.privilege from {$config['table_prefix']}admin_user as u left join {$config['table_prefix']}admin_group as g on u.group_id=g.id where u.name = ?";
				$res = $pdo->dql($sql,array($after_user),1);
				if($res){
					$_SESSION['privileges'] = $res['privilege'];
				}else{
					Token_Void($switch_type);	//作废token值
					exit('err2');		//没有任何权限
				}
			}
			$priv_arr = explode(',',$_SESSION['privileges']);
			if(in_array($priv_name,$priv_arr)){
				echo 'ok';
			}else{
				echo 'err3';	//没有权限
			}
		}
	}

	break;








	//删除等重要操作的口令比对
	case 'After_Del_Con_Pwd_Diff':
		$pwd = $_POST['pwd'];
		if($pwd == $config['del_pwd']){
			echo 'ok';
		}else{
			echo 'err';
		}
	break;






	//设置token
	case 'token':
		$name = $_POST['token_name'];
		$_SESSION[$name] = md5(microtime().rand(1,99999)).sha1(rand(99999,999999));
		echo $_SESSION[$name] ;
	break;




	//qq登陆
	case 'qq_login':
		require 'Class/Qq/qqConnectAPI.php';
		$qc = new QC();
		$qc->qq_login();
	break;


	//微博登陆
	case 'weibo_login':
		echo 'weibo';
	break;


	//清除全部cookie
	case 'clear_cookie':
		var_dump($_COOKIE);
		foreach($_COOKIE as $k=>$v){
			setCookie($k,'',time()-1);
		}
	break;




	//验证码图片
	case 'code':
		header("content-type:image/jpeg");
		$img_width = 200;	//图片宽度
		$img_height = 50;  //图片高度
		$img=imagecreatetruecolor($img_width,$img_height);
		$bgcolor=imagecolorallocate($img,rand(125,255),rand(125,255),rand(125,255));
		imagefill($img,0,0,$bgcolor);
		//画杂点
		for($i=1;$i<500;$i++){
			$colord=imagecolorallocate($img,rand(0,125),rand(0,125),rand(0,125));
			$x=rand(5,$img_width);
			$y=rand(5,$img_height);
			imagesetpixel($img,$x,$y,$colord);
		}
		//画杂线
		for($i=1;$i<10;$i++){
			$colord=imagecolorallocate($img,rand(0,125),rand(0,125),rand(0,125));
			$x=rand(0,$img_width);	//起点的X轴
			$y=rand(0,$img_height); //起点的Y轴
			$x2=rand(0,$img_width);	//起点的X轴
			$y2=rand(0,$img_height); //起点的Y轴
			imageline($img,$x,$y,$x2,$y2,$colord);		//画一条线
		}
		$str='ABCDEFGHJKLMNPQRSTUVWXYZ0123456789';
		$sess_str = '';
		for($j=1;$j<=4;$j++){
			$colort=imagecolorallocate($img,rand(1,125),rand(1,125),rand(1,125));
			$size=rand($img_height*0.6,$img_height*0.8);	//文字大小
			$text=$str[rand(0,strlen($str)-1)];
			$sess_str .= $text;
			$info=imagettfbbox($size,0,'./Public/ahronbd.ttf',$text);
			$x=($img_width/6)*$j;
			//$y = $img_height - abs($info[7] - $info[0]);//随机处来的y的位置
			//$y = $img_height - abs($info[7] - $info[1]);//随机处来的y的位置
			$y=rand($size,$img_height);
			imagettftext($img,$size,0,$x,$y,$colort,'./Public/ahronbd.ttf',$text);
		}
		$_SESSION['code'] = strtolower($sess_str);	//将验证码字串存放到session
		imagejpeg($img);
		imagedestroy($img);
	break;








	//检测验证码
	case 'Verify_Code':
		if($_COOKIE['uid'] == 1){
			echo 'ok';
		}else{
			if(!Verify_Code()){
				echo 'err';
			}else{
				echo 'ok';
			}
		}
	break;







}

Token_Void($switch_type);	//作废token值




